package com.infore.airsupervise.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.beardedhen.androidbootstrap.font.FontAwesome;
import com.bigkoo.pickerview.TimePickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.infore.airsupervise.R;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.bean.MenuE;
import com.infore.airsupervise.ui.widget.SwipeRecyclerView;
import com.infore.airsupervise.utils.DateUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 勘察历史界面
 * Created by xiaotao on 2017/8/10.
 */

public class SurveyHistoryActivity extends BaseTitleBarActivity implements SwipeRecyclerView.OnActionListener {

    @BindView(R.id.tv_startTime)
    TextView tvStartTime;
    @BindView(R.id.tv_endTime)
    TextView tvEndTime;
    @BindView(R.id.refresh_recycler_view)
    SwipeRecyclerView recyclerView;

    private TimePickerView timePickerView= null; //时间选择器
    private TextView tv_title = null ; //时间选择器标题
    private BaseQuickAdapter<String,BaseViewHolder>  adapter = null;

    private String startTime;
    private String endTime;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_survey_history);
        ButterKnife.bind(this);
        Intent it = getIntent();
        if (it != null) {
            MenuE menuE = (MenuE) it.getSerializableExtra(Constants.INTENT_OBJ_KEY);
            if (null != menuE) {
                //titleBar
                title.setText(menuE.getTitle());
            }
        }
        initView();
    }

    private void initView() {
        rightMenu.setFontAwesomeIcon(FontAwesome.FA_SEARCH);
        //时间
        startTime = DateUtil.getStringByOffset(Calendar.getInstance().getTime(),DateUtil.FORMAT_DATE,Calendar.DAY_OF_MONTH,-1);
        endTime = DateUtil.getCurrentDateStr(DateUtil.FORMAT_DATE);
        tvStartTime.setText(startTime);
        tvEndTime.setText(endTime);
        //RecyclerView
        initRecyclerView();
    }

    /**
     * 初始化列表控件 适配器
     */
    private void initRecyclerView(){
        adapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_survey_history,null) {
            @Override
            protected void convert(BaseViewHolder helper, String item) {

            }
        };
        recyclerView.setActionListener(this);
        recyclerView.setAdapter(adapter);

        adapter.addData("");
        adapter.addData("");
        adapter.addData("");
        adapter.loadMoreEnd();
    }

    /**
     * 初始化时间选择器
     */
    private void initTimePicker(){
        Calendar endSelectedDate = Calendar.getInstance();
        timePickerView = new TimePickerView.Builder(ctx, timePickerListener)
                .setTitleText("")
                .setLabel("年", "月", "日", "点", "分", "秒")
                .setType(new boolean[]{true,true,true,false,false,false})
                .isCenterLabel(false)
                .setDividerColor(Color.DKGRAY)
                .setContentSize(18)
                .setRangDate(null, endSelectedDate)
                .setDate(endSelectedDate)
                .setDecorView(null)
                .build();
        tv_title = (TextView) timePickerView.findViewById(com.bigkoo.pickerview.R.id.tvTitle);
    }

    /**
     * 点击事件
     * @param view
     */
    @OnClick({R.id.tv_startTime, R.id.tv_endTime})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_startTime://时间
            case R.id.tv_endTime:
                if(timePickerView == null){
                    initTimePicker();
                }
                timePickerView.show(view);
                break;
        }
    }

    /**
     * 时间选择器回调方法
     */
    private TimePickerView.OnTimeSelectListener timePickerListener= new TimePickerView.OnTimeSelectListener() {
        @Override
        public void onTimeSelect(Date date, View v) {
            switch (v.getId()){
                case R.id.tv_startTime:
                    startTime =  DateUtil.getStringByFormat(date,DateUtil.FORMAT_DATE);
                    tvStartTime.setText(startTime);
                    break;
                case R.id.tv_endTime:
                    endTime = DateUtil.getStringByFormat(date,DateUtil.FORMAT_DATE);
                    tvEndTime.setText(endTime);
                    break;
            }
        }
    };

    @Override
    public void onRefresh() {
        recyclerView.commonSuccessHandle(true,3);
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }
}
